﻿using System;
using System.Net;
using System.ComponentModel;
using System.Xml.Serialization;

namespace AdRotator.Common
{

    public abstract partial class AdProviderBase: IAdProvider, INotifyPropertyChanged
    {
        #region Fields

        private double _probability;

        private bool _isDefault, _isTest;

        public event PropertyChangedEventHandler PropertyChanged;

        public event AdRotator.Common.LogHandler Log;

        public event EventHandler AdLoadingError;

        #endregion

        #region Properties

        [XmlAttribute("Probability")]
        /// <summary>
        /// The probability to show the ad. This can be any double, though for simplicity
        ///     reasons it's advised that all Probability values in <see cref='AdCulture'/> 
        ///     add up to 100.
        /// </summary>
        public double Probability
        {
            get
            {
                return _probability;
            }
            set
            {
                if (_probability != value)
                {
                    _probability = value;
                    NotifyPropertyChanged("Probability");
                }
            }
        }

        [XmlAttribute("IsDefault")]
        /// <summary>
        /// Whether or not this is the default ad provider to show
        /// </summary>
        public bool IsDefault
        {
            get
            {
                return _isDefault;
            }
            set
            {
                if (_isDefault != value)
                {
                    _isDefault = value;
                    NotifyPropertyChanged("IsDefault");
                }
            }
        }

        [XmlAttribute("IsTest")]
        /// <summary>
        /// The probability to show the ad. This can be any double, though for simplicity
        ///     reasons it's advised that all Probability values in <see cref='AdCulture'/> 
        ///     add up to 100.
        /// </summary>
        public bool IsTest
        {
            get
            {
                return _isTest;
            }
            set
            {
                if (_isTest != value)
                {
                    _isTest = value;
                    NotifyPropertyChanged("IsTest");
                }
            }
        }

        public abstract bool IsValid { get; }

        #endregion

        public abstract void Initialize();

        public abstract void Deinitialize();

        protected void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        
        protected void RaiseLogEvent(string message)
        {
            if (Log != null)
            {
                Log(message);
            }
        }

        protected void RaiseLogEvent(string message, Exception ex)
        {
            if(ex == null)
            {
                Log(message);
            }
            if (Log != null)
            {
                Log(String.Format("{0}, exception message: {1}, stack trace: {2}", message, ex.Message, ex.StackTrace));
            }
        }

        protected void RaiseAdLoadingErrorEvent()
        {
            if (AdLoadingError != null)
            {
                AdLoadingError(this, new EventArgs());
            }
        }
        
    }
}
